package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCertCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCertForKeepCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.receive.cmd.BillHldrCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillRcv4PrntCmd;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevCallbackChdSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbDct>> {
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private BillDctCertCmd billDctCertCmd;
	@Autowired
	private BillDctCertForKeepCmd billDctCertForKeepCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private BillHldrCmd<CbDct> billHldrCmd;
	@Autowired
	private BillRcv4PrntCmd<CbDct> billRcv4PrntCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbDct>> tranCallbackCmd;

	public void onApplicationEvent(BillTranEvent<CbDct> t) {
		List<Bill<CbDct>> child = (List<Bill<CbDct>>) t.getChildren();
		AcctTxRec tran = t.getTran();
		String createTime = null;
		for (Bill<CbDct> bill : child) {
			CbDct cbDct = (CbDct) bill.getSource();
			String ct = cbDct.getCreateTime();
			if ((createTime == null) && (ct != null) && (!"".equals(ct))) {
				createTime = ct;
			}
		}
		for (Bill<CbDct> bill : child) {
			((CbDct) bill.getSource()).setCreateTime(createTime);

			CbOpt opt = new CbOpt();
			if (((CbDct) bill.getSource()).getDctId() == null) {
				opt.setCurState("S000");
				opt.setOptCode("E503");
				opt.setRemark("����������������������������");
			} else {
				opt.setCurState("S000");
				opt.setOptCode("E504");
				opt.setRemark("����������������������������");
			}
			CallbackContext<Bill<CbDct>, AcctTxRec> context = new CallbackContext();
			context.setSource(bill);
			context.setTarget(tran);
			context.setValue("Operation", opt);

			ChainCallback<Bill<CbDct>, AcctTxRec> ccb = new ChainCallback();
			ccb.add(this.billOptCmd);
			if (((CbDct) bill.getSource()).getDctId() == null) {
				String buyerId = null;
				String buyerName = null;
				for (Bill<CbDct> billChd : child) {
					if (((CbDct) billChd.getSource()).getDctId() != null) {
						buyerId = ((CbDct) billChd.getSource()).getBuyerId();
						buyerName = ((CbDct) billChd.getSource()).getBuyerNm();
						break;
					}
				}
				context.setValue("OwnerId", buyerId);
				context.setValue("OwnerName", buyerName);
				ccb.add(this.billHldrCmd);
				ccb.add(this.billRcv4PrntCmd);
				ccb.add(this.billDctCertForKeepCmd);
			} else {
				context.setValue("OwnerId", ((CbDct) bill.getSource()).getFinId());
				context.setValue("OwnerName", ((CbDct) bill.getSource()).getFinNm());
				ccb.add(this.billHldrCmd);

				ccb.add(this.billDctCmd);
				ccb.add(this.billDctCertCmd);
				ccb.add(this.tranCallbackCmd);
			}
			ccb.add(this.billMoveCmd);

			ccb.callback(context);
		}
	}
}
